//查询接口
 function  account (app,services){
  app.get('/account', async function(req, res){
    // req请求对象，可以获取请求传递过来的信息
    // res响应对象，通过该对象可以控制响应的消息内容
    const {page= 1,limit= 10,keyword = ''} = req.query
    let result = await services.accountServe.getAccount(page, limit, keyword);
    res.send({ code: 200, message: "请求成功",data: result });

  })
  app.delete('/account/:id', async function(req, res) {
    const { id } = req.params; // 从URL参数中获取新闻ID
    try {
      let success = await services.accountServe.deleteAccount(id); // 调用服务函数删除新闻
      if (success) {
        res.send({ code: 200, message: "删除成功" });
      } else {
        res.send({ code: 404, message: "用户未找到" });
      }
    } catch (error) {
      res.send({ code: 500, message: "服务器错误", error: error.message });
    }
  });
// 更改启/禁用状态
  app.put('/updateAccount/:id', async function(req, res) {
    const { id } = req.params; // 从URL参数中获取新闻ID
    const { status } = req.body; // 从URL参数中获取新闻ID
    console.log('id',id)
    console.log('status',status,res.body)
    try {
      let success = await services.accountServe.putStatus(id,status); // 调用服务函数删除新闻
      if (success) {
        res.send({ code: 200, message: "更改成功" });
      } else {
        res.send({ code: 400, message: "更改失败" });
      }
    } catch (error) {
      res.send({ code: 500, message: "服务器错误", error: error.message });
    }
  });

  //创建用户
   app.post('/createAccount', async function(req, res) {
    const obj = req.body;
     let success = await services.accountServe.createAccount(obj); // 创建新闻
     if (success) {
       res.send({ code: 200, message: "增加成功" });
     }else {
       res.send({ code: 400, message: "增加失败" });
     }
   })

   app.post('/update/:id', async function(req, res) {
    const obj = req.body
     try {
       let success = await services.accountServe.updateAccount(obj.id,obj); // 调用服务函数删除新闻
       if (success) {
         res.send({ code: 200, message: "更改成功" });
       } else {
         res.send({ code: 400, message: "更改失败" });
       }
     } catch (error) {
       res.send({ code: 500, message: "服务器错误", error: error.message });
     }
   });

}

module.exports = account
